package my.sorter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import my.sorter.bean.EmergencyCase;
import my.sorter.bean.MyPageInfo;
import my.sorter.bean.filter.EmergencyCaseFilter;
import my.sorter.dao.model.Diagnoses;
import my.sorter.service.SortService;
import my.sorter.translator.out.MyMessage;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author zyf10
 */
@Controller
public class SortController {
    @Autowired
    SortService sortService;

    /**
     * 新增病例和更新病例
     *
     * @param diagnoses
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/createOrUpdate")
    @ResponseBody
    public Object createDiagnoses(Diagnoses diagnoses) {
        Boolean flag = false;
        String message = null;
        if (StringUtils.isEmpty(diagnoses.getId())) {
//            新增
            diagnoses.setId(UUID.randomUUID().toString().replace("-", ""));
            diagnoses.setArriveTime(new Date());
            diagnoses.setActive(1);
            flag = sortService.createDiagnoses(diagnoses);
            message = "新增病例";
        } else {
//            更新
            Diagnoses temp = new Diagnoses();
            temp.setId(diagnoses.getId());
            temp.setpName(diagnoses.getpName());
            temp.setIdNumber(diagnoses.getIdNumber());
            temp.setArriveChannel(diagnoses.getArriveChannel());
            flag = sortService.updateDiagnosesBasic(temp);
            message = "信息补录";
        }
        return flag ? MyMessage.successMessage().addMessage(message + "成功") : MyMessage.failMessage().addMessage(message + "失败");
    }

    /**
     * 获取病例完整信息
     *
     * @param filter
     * @param myPageInfo
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @GetMapping("/diagnoses/get")
    @ResponseBody
    public Object getDiagnoses(EmergencyCaseFilter filter, MyPageInfo myPageInfo) {
        PageHelper.startPage(myPageInfo.getCurrentPage(), myPageInfo.getPageSize());
        List<EmergencyCase> emergencyCases = sortService.getDiagnoses(filter);
        PageInfo pageInfo = new PageInfo(emergencyCases);
        myPageInfo.setTotal((new Long(pageInfo.getTotal()).intValue()));
        myPageInfo.setTotalPage(pageInfo.getPages());
        return MyMessage
                .successMessage(myPageInfo)
                .addMessage("获取病例完整信息成功")
                .addData(emergencyCases);
    }

    /**
     * 关闭病例
     *
     * @param diagnosesIds
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/close")
    @ResponseBody
    public Object closeDiagnoses(String diagnosesIds) {
        JSONArray idsArr = JSONArray.parseArray(diagnosesIds);
        List<String> ids = new ArrayList<>();
        for (Object id : idsArr) {
            ids.add(id.toString());
        }
        Boolean flag = sortService.closeDiagnoses(ids);
        return flag ? MyMessage.successMessage("关闭病例成功") : MyMessage.failMessage("关闭病例失败");
    }

    /**
     * 添加护理评估
     *
     * @param diagnoses
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/sign/create")
    @ResponseBody
    public Object signJudge(Diagnoses diagnoses) {
        Boolean flag = sortService.signJudge(diagnoses);
        return flag ? MyMessage.successMessage().addMessage("添加护理评估成功") : MyMessage.failMessage().addMessage("添加护理评估失败");
    }

    /**
     * 更新护理评估
     *
     * @param diagnoses
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/sign/update")
    @ResponseBody
    public Object updateSignJudge(Diagnoses diagnoses) {
        Boolean flag = sortService.signJudge(diagnoses);
        return flag ? MyMessage.successMessage().addMessage("更新护理评估成功") : MyMessage.failMessage().addMessage("更新护理评估失败");
    }

    /**
     * 清空护理评估信息
     *
     * @param diagnosesId
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @GetMapping("/diagnoses/sign/delete")
    @ResponseBody
    public Object deleteSignJudge(String diagnosesId) {
        Boolean flag = sortService.removeSignJudge(diagnosesId);
        return flag ? MyMessage.successMessage().addMessage("清空护理评估成功") : MyMessage.failMessage().addMessage("清空护理评估失败");
    }

    /**
     * 增加病情等级评估
     *
     * @param diseaseString
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/disease/create")
    @ResponseBody
    public Object diseaseJudge(String diseaseString) {
        JSONObject dieaseJson = JSON.parseObject(diseaseString);
        String diagnosesId = dieaseJson.get("diagnosesId").toString();
        JSONArray detailsJson = dieaseJson.getJSONArray("details");
        List<JSONObject> details = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < detailsJson.size(); i++) {
            details.add(detailsJson.getJSONObject(i));
        }
        Boolean flagRemove = sortService.removeSignJudge(diagnosesId);
        Boolean flagUpdate = sortService.diseaseJudge(diagnosesId, details);
        return (flagRemove && flagUpdate) ? MyMessage.successMessage().addMessage("添加病情评估成功") : MyMessage.failMessage().addMessage("添加病情评估失败");
    }
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @PostMapping("/diagnoses/disease/update")
    @ResponseBody
    public Object updateDiseaseJudge(String diseaseString) {
        JSONObject diseaseJson = JSON.parseObject(diseaseString);
        String diagnosesId = diseaseJson.get("id").toString();
        JSONArray detailsJson = diseaseJson.getJSONArray("details");
        List<JSONObject> details = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < detailsJson.size(); i++) {
            details.add(detailsJson.getJSONObject(i));
        }
        Boolean flagRemove = sortService.deleteDiseaseJudge(diagnosesId);
        Boolean flagUpdate = sortService.diseaseJudge(diagnosesId, details);
        return (flagRemove && flagUpdate) ? MyMessage.successMessage().addMessage("添加病情评估成功") : MyMessage.failMessage().addMessage("添加病情评估失败");
    }

    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @GetMapping("/diagnoses/disease/delete")
    @ResponseBody
    public Object deleteDiseaseJudge(String diagnosesId) {
        Boolean flag = sortService.deleteDiseaseJudge(diagnosesId);
        return flag ? MyMessage.successMessage("删除病情评估成功") : MyMessage.failMessage("删除病情评估成功");
    }

    /**
     * 计算就诊顺序
     * @param idNumber
     * @return
     */
    @RequiresPermissions(value = {"admin","diseaseJudge","signJudge"},logical = Logical.OR)
    @GetMapping("/diagnoses/order")
    @ResponseBody
    public Object calculateOrder(String idNumber){
        Integer order = sortService.calculateOrder(idNumber);
        return MyMessage.successMessage("获取顺序成功").addData(order);
    }
}
